package com.hudongwx.shansun.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.hudongwx.shansun.R;
import com.hudongwx.shansun.loginFragmeng.LoginFragment;
import com.hudongwx.shansun.loginFragmeng.RegisterFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity {
    @ViewInject(R.id.login_tab)
    private TabLayout tabLayout;
    @ViewInject(R.id.login_view)
    private ViewPager viewPager;
    private Fragment[] fragments = new Fragment[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        TabLayout.Tab tab1 = tabLayout.newTab().setText("登陆");
        TabLayout.Tab tab2 = tabLayout.newTab().setText("注册");
        tabLayout.addTab(tab1);
        tabLayout.addTab(tab2);
        tabLayout.setupWithViewPager(viewPager);
        LoginAdapter loginAdapter = new LoginAdapter(getSupportFragmentManager());
        viewPager.setAdapter(loginAdapter);

    }

    class LoginAdapter extends FragmentStatePagerAdapter {
        public LoginAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (null == fragments[position]) {
                        fragments[position] = new LoginFragment();
                    }
                    break;
                case 1:
                    if (null == fragments[position]) {
                        fragments[position] = new RegisterFragment();
                    }
                    break;
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (0 == position) {
                return "登陆";
            } else if (1 == position) {
                return "注册";
            }
         return null;
        }
    }

    @Event(R.id.login_iv)
    private void ivBack(View view) {
        finish();
    }
}
